//
//  SWMenueView.m
//  YiYueHairdresser
//
//  Created by Sky on 2018/10/29.
//  Copyright © 2018年 YuYiLian. All rights reserved.
//

#import "SWMenueView.h"

@interface SWMenueView()
@property (nonatomic, strong) NSMutableArray * arrBtns;
@property (nonatomic, strong) NSArray*services;
@end
@implementation SWMenueView

- (void)setServiceArray:(NSArray *) services {
    _services = services;
    _arrBtns = [NSMutableArray arrayWithCapacity:services.count];
    for (UIView*view in self.subviews) {
        [view removeFromSuperview];
    }
    
    int y = 0;
    int x = 0;
    CGFloat left = 20;
    CGFloat top = 10;
    CGFloat space = 15;
    CGFloat width = 80;
    CGFloat height = 30;
    CGFloat limt_X = self.width - 2 *left;
    CGFloat maY = 0;
    [self removeAllSubviews];
    [_arrBtns removeAllObjects];
    for (int i = 0; i < services.count; i ++) {
        
        STServiceCategoryModel * model =services[i];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(left + (space * x) + (x * width), top + (space * y) + (y * height), width, height);
        if (CGRectGetMaxX(btn.frame) > limt_X) {
            x = 0;
            y ++;
            btn.frame = CGRectMake(left + (space * i) + (i * width), top + (space * y) + (y * height), width, height);
        
        }
        maY = CGRectGetMaxY(btn.frame);
        x ++;
        btn.tag = i;
        btn.layer.borderColor = UIColor.lightGrayColor.CGColor;
        btn.layer.borderWidth = 0.5;
        btn.layer.masksToBounds = YES;
        btn.layer.cornerRadius = 3;
        [btn setTitle:model.name forState:UIControlStateNormal];
        [btn setTitleColor:UIColor.grayColor forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        [btn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        [_arrBtns addObject:btn];
    }
    self.height = maY + top;
}

- (void)clickAction:(UIButton *)sender {
    [self setBtn:(int)sender.tag selStatus:!sender.selected];
    self.BackSelectBlock((int)sender.tag);
}


-(void)setBtn:(int)num selStatus:(BOOL)status{
    if(!status){
        UIButton * currentBtn = _arrBtns[num];
        currentBtn.selected = status;
        [self changeBtnStatus:currentBtn];
        return;
    }
    
    for (UIButton * currentBtn in _arrBtns) {
        if (currentBtn.tag == num) {
            currentBtn.selected = YES;
            [self changeBtnStatus:currentBtn];
        }else{
            currentBtn.selected = NO;
            [self changeBtnStatus:currentBtn];
        }
    }
}

-(void)changeBtnStatus:(UIButton *)sender{
    if (sender.selected) {
        [sender setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        sender.backgroundColor = UIColor.orangeColor;
    } else {
        [sender setTitleColor:UIColor.grayColor forState:UIControlStateNormal];
        sender.backgroundColor = UIColor.whiteColor;
    }
}

@end
